home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Subject: Re: warning: possibly inc
- X-Nntp-Posting-Host: golden.ripco.com
- Message-ID: <DKy500.2q2@rci.ripco.com>
- Sender: usenet@rci.ripco.com (Net News Admin)
- Organization: Ripco Internet BBS Chicago
- Date: Wed, 10 Jan 1996 03:43:11 GMT
- X-Ident-Sender: mambuhl
-
- Bill Simpson <wsimpson@uwinnipeg.ca>
- in <Pine.OSF.3.91.960109091920.6447A-100000@io.UWinnipeg.ca> asks:
-
- >I get the above warning
- [warning: possibly incorrect assignment]
- >when I compile code using the following
- >function. It flags the **** line.
-
- >**** while(fp=fopen(file_name,"r"))
- use:
- while((fp=fopen(file_name,"r")))
-
- >How can I write this code so the compiler does not issue this warning?
- >Please don't suggest I just tell the compiler to shut up. In general
- >the warnings are useful. I do not want to just ignore the warning either.
- >I have the FAQ and haven't seen this discussed.
-
- While you are absolutely right to not ignore warnings, there is a
- difference between turning off warnings and doing what is necessary in
- the code to suppress them.
-
- In this case, you are using an assignment where it is common to have
- have a comparison, e.g.:
- while(fp==fopen(file_name,"r"))
- This warning tells you that you may not be doing what you intend. The
- normal way to tell the compiler that you know what you are doing is to
- enclose the assignment in ()s so the compiler see a while statement like
- while(result_of_assignment)
-
- You could add an explicit comparison to the suggested change
- while((fp=fopen(file_name,"r")) != NULL)
- but this is a style issue only. The two forms are functionally the
- same. For example, my compiler produces exactly the same code (below)
- for each:
-
- L5:
- pushl $LC1
- pushl _file_name
- call _fopen
- movl %eax,_fp
- addl $8,%esp
- testl %eax,%eax
- je L6
- ; while block goes here
- jmp L5
- .align 2,0x90
- L6:
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-